package com.farmshop.www.swiperefreshlayoutdemo;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.farmshop.www.swiperefreshlayoutdemo.adapter.mAdapter;
import com.farmshop.www.swiperefreshlayoutdemo.base.BaseActivity;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

public class MainActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,mAdapter.OnItemClickListener {

    //AppCompatActivity mActivity = this;
    @ViewInject(R.id.list_recyclerView)
    RecyclerView recyclerView;
    @ViewInject(R.id.id_swipe_ly)
    SwipeRefreshLayout swipeRefreshLayout;
    ArrayList<String> mDatas;
    mAdapter madapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //recyclerView = (RecyclerView) findViewById(R.id.list_recyclerView);
        //swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
        swipeRefreshLayout.setOnRefreshListener(this);
        initData();
        //ArrayList<Map<String,String>> arrayList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        madapter = new mAdapter(this,mDatas);
        setListener();
    }

    @Override
    public void onRefresh() {
        Toast.makeText(this, "正在加载", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 'A'; i <= 'z'; i++) {
                    mDatas.add("" + (char) i);
                }
                madapter.notifyDataSetChanged();
                swipeRefreshLayout.setRefreshing(false);
            }
        }, 1000);

    }

    protected void initData() {
        mDatas = new ArrayList<>();
        for (int i = 'A'; i < 'z'; i++) {
            mDatas.add("" + (char) i);
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        Toast.makeText(this,"点击当前："+position,Toast.LENGTH_LONG).show();
    }

    @Override
    public void setListener() {
        madapter.setOnItemClickListener(this);
        recyclerView.setAdapter(madapter);
    }
}
